package com.sskj.one.mine;

import android.arch.lifecycle.Observer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.one.R;
import com.sskj.one.component.DaggerUserDataComponent;
import com.sskj.one.view.BottomDialog;

import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/10
 */
public class AddBankActivity extends BaseActivity<AddBankPresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.et_branch)
    EditText etBranch;
    @BindView(R.id.et_bank_card)
    EditText etBankCard;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.ll_pay_pwd)
    LinearLayout ll_pay_pwd;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @Inject
    UserViewModel userViewModel;

    private String bankID;

    private UserData userData;
    private List<String> banks;
    private Map<String, String> bankIDs = new HashMap<>();

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_add_bank;
    }

    @Override
    public AddBankPresenter getPresenter() {
        return new AddBankPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("添加结汇方式");
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                if (isEmpty(userData.getBank_nums())) {
                    tvTitle.setText("添加结汇方式");
                } else {
                    tvTitle.setText("结汇方式");
                    etName.setEnabled(false);
                    tvBank.setEnabled(false);
                    etBankCard.setEnabled(false);
                    etBranch.setEnabled(false);
                    etName.setText(userData.getBank_username());
                    tvBank.setText(userData.getBank_name());
                    etBankCard.setText(userData.getBank_nums());
                    etBranch.setText(userData.getBank_sub_name());
                    view.setVisibility(View.GONE);
                    btnConfirm.setVisibility(View.GONE);
                    ll_pay_pwd.setVisibility(View.GONE);
                }
            }

        });
    }

    @Override
    public void initData() {
        banks = new ArrayList<>();
        banks.add("工商银行");
        banks.add("建设银行");
        banks.add("农业银行");
        banks.add("交通银行");
        banks.add("招商银行");
        banks.add("浦发银行");
        banks.add("兴业银行");
        banks.add("中国银行");
        banks.add("邮政储蓄");
        banks.add("中信银行");
        banks.add("民生银行");
        banks.add("广大银行");
        banks.add("华夏银行");
        banks.add("平安银行");
        banks.add("广发银行");
        bankIDs.put("工商银行", "icbc");
        bankIDs.put("建设银行", "ccb");
        bankIDs.put("农业银行", "abc");
        bankIDs.put("交通银行", "comm");
        bankIDs.put("招商银行", "cmb");
        bankIDs.put("浦发银行", "spdb");
        bankIDs.put("兴业银行", "cib");
        bankIDs.put("中国银行", "boc");
        bankIDs.put("邮政储蓄", "post");
        bankIDs.put("中信银行", "ecitic");
        bankIDs.put("民生银行", "cmbc");
        bankIDs.put("广大银行", "ceb");
        bankIDs.put("华夏银行", "hxb");
        bankIDs.put("平安银行", "pab");
        bankIDs.put("广发银行", "gdb");
        ClickUtil.click(300, btnConfirm, () -> {
            if (userData == null) {
                ToastUtils.show("获取用户信息失败");
                return;
            }
            if (isEmpty(etName)) {
                toastHint(etName);
                return;
            }
            if (getText(tvBank).equals("请选择开户行名称") || isEmpty(bankID)) {
                ToastUtils.show("请选择开户行名称");
                return;
            }
            if (isEmpty(etBranch)) {
                toastHint(etBranch);
                return;
            }
            if (isEmpty(etBankCard)) {
                toastHint(etBankCard);
                return;
            }
            if (getText(etBankCard).length() < 14) {
                ToastUtils.show("请输入正确的银行卡号");
                return;
            }
            if (isEmpty(etPayPwd)) {
                toastHint(etPayPwd);
                return;
            }
            mPresenter.addBank(getText(etName), getText(tvBank), bankID, getText(etBranch), getText(etBankCard), getText(etPayPwd));
        });
        ClickUtil.click(300, tvBank, () -> {
            if (banks == null || banks.size() == 0) {
                return;
            }
            BottomDialog.showShopCheck(AddBankActivity.this, banks, tv -> {
                tvBank.setText(tv);
                bankID = bankIDs.get(tv);
            });
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AddBankActivity.class);
        context.startActivity(intent);
    }

    public void addBankOK(String name, String bank, String branch, String card) {
        userData.setBank_username(name);
        userData.setBank_name(bank);
        userData.setBank_sub_name(branch);
        userData.setBank_nums(card);
        userViewModel.update(userData);
        finish();
    }
}
